package com.example.dragon.adpter;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dragon.bean.OrderBean;
import com.example.dragon.common.Common;
import com.example.dragon.dialog.MessageDialog;
import com.example.dragon.untis.CircleImageView;
import com.example.dragon.xmf.MainActivity;
import com.example.dragon.xmf.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okhttp3.Call;

/**
 * Created by Dragon on 2017/6/22.
 */

public class ServiceAdpter extends RecyclerView.Adapter {
    private List<OrderBean> orderBeanList = new ArrayList<>();
    Context mcontext;
    MessageDialog messageDialog;
    public ServiceAdpter(Context mcontext,List<OrderBean> orderBeanList) {
        this.mcontext = mcontext;
        this.orderBeanList=orderBeanList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mcontext).inflate(R.layout.pending_item_layout, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final OrderBean item = orderBeanList.get(position);

        final ViewHolder viewHolder = (ViewHolder) holder;
        if (item != null) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
            Date curDate =  new Date(System.currentTimeMillis());
            String   str   =   formatter.format(curDate);

            viewHolder.isdateText.setText(str);
           // viewHolder.isdnameText.setText(item.getDname());
            viewHolder.isFareText.setText(item.getFare());
            viewHolder.isoidText.setText(item.getOrders_id());
            viewHolder.isAddressText.setText(item.getAddress());
            Glide.with(mcontext).load(item.getAvatar()).error(R.drawable.order_icon).into(viewHolder.isAvter);
            viewHolder.isdnameText.setText(item.getDname());
            viewHolder.ratingBar.setRating(2);
            if (item.getStatus().equals("1")){
                viewHolder.istutdsText.setText("待发车");
            }else if (item.getStatus().equals("2")){
                viewHolder.istutdsText.setText("在路上");
                viewHolder.isCanleBtn.setText("司机到达");
                viewHolder.isremindBtn.setVisibility(View.GONE);
            }else if (item.getStatus().equals("3")){
                viewHolder.istutdsText.setText("已到达");
                viewHolder.isCanleBtn.setText("工作完成");
                viewHolder.isremindBtn.setVisibility(View.GONE);
            }
            viewHolder.isCanleBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (item.getStatus().equals("1")){
                        canleOrder(item.getOrders_id());
                    }else if (item.getStatus().equals("2")){
                        deriveArrive(item.getOrders_id());
                    }else if (item.getStatus().equals("3")){
                        orederfinish(item.getOrders_id());
                    }
                }
            });
        }
    }
    private void deriveArrive(String oid){
        OkHttpUtils.post()
                .url(Common.MIFENG_URL+"Orders&a=reach")
                .addParams("token", MainActivity.TOKEN)
                .addParams("orders_id",oid)
                .build()//
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.i("res",e.toString());
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("res",response.toString());
                        try {
                            JSONObject jsonObject =new JSONObject(response);
                            int code = jsonObject.getInt("code");
                            if (code==200){
                                messageDialog = new MessageDialog("",jsonObject.getString("message"),"确定",mcontext);
                                notifyDataSetChanged();
                            }else {
                                messageDialog = new MessageDialog("",jsonObject.getString("message"),"确定",mcontext);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void orederfinish(String oid){
        OkHttpUtils.post()
                .url(Common.MIFENG_URL+"Orders&a=finish")
                .addParams("token", MainActivity.TOKEN)
                .addParams("orders_id",oid)
                .build()//
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.i("res",e.toString());
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("res",response.toString());
                        try {
                            JSONObject jsonObject =new JSONObject(response);
                            int code = jsonObject.getInt("code");
                            if (code==200){
                                messageDialog = new MessageDialog("",jsonObject.getString("message"),"确定",mcontext);
                                notifyDataSetChanged();
                            }else {
                                messageDialog = new MessageDialog("",jsonObject.getString("message"),"确定",mcontext);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void canleOrder(String oid){
        OkHttpUtils.post()
                .url(Common.MIFENG_URL+"Orders&a=cancel")
                .addParams("token", MainActivity.TOKEN)
                .addParams("orders_id",oid)
                .build()//
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.i("res",e.toString());
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("res",response.toString());
                        try {
                            JSONObject jsonObject =new JSONObject(response);
                            int code = jsonObject.getInt("code");
                            if (code==200){
                                messageDialog = new MessageDialog("",jsonObject.getString("message"),"确定",mcontext);
                                notifyDataSetChanged();
                            }else {
                                messageDialog = new MessageDialog("",jsonObject.getString("message"),"确定",mcontext);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
    @Override
    public int getItemCount() {
        return orderBeanList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView isdateText,isAddressText,isoidText,isFareText,isdnameText,istutdsText;
        CircleImageView isAvter;
        Button isremindBtn,isCanleBtn;
        RatingBar ratingBar;
        public ViewHolder(View itemView) {
            super(itemView);
            ratingBar = (RatingBar)itemView.findViewById(R.id.isratbar);
            isAddressText =(TextView)itemView.findViewById(R.id.isAdressText);
            isdateText =(TextView)itemView.findViewById(R.id.isdateText);
            isoidText =(TextView)itemView.findViewById(R.id.isorderIdText);
            isFareText =(TextView)itemView.findViewById(R.id.isqibuText);
            isdnameText =(TextView)itemView.findViewById(R.id.isavterText);
            isAvter =(CircleImageView)itemView.findViewById(R.id.isavterimg);
            isremindBtn =(Button)itemView.findViewById(R.id.isReminderBtn);
            isCanleBtn =(Button)itemView.findViewById(R.id.iscancelorderBtn);
            istutdsText = (TextView)itemView.findViewById(R.id.isstatusText);
        }
    }
}
